package com.content.scaffold.controller.base;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.service.IService;
import com.content.scaffold.common.mybatis.PageParams;
import com.content.scaffold.common.utils.ApprenticeUtil;
import com.content.scaffold.common.web.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaojl
 */
@RestController
@Slf4j
public class BaseController<S extends IService<E>, E> {

    private final int PAGE = 1;
    private final int SIZE = 100;
    @Autowired
    protected S baseService;


    /**
     * 增
     */
    @PostMapping("/insert")
    public ResultBody<E> insert(@RequestBody E entity) {
        baseService.save(entity);
        return ResultBody.ok(entity);
    }

    /**
     * 删
     **/
    @PostMapping("/deleteById")
    public ResultBody<String> delete(@RequestBody List<Long> ids) {
        baseService.removeByIds(ids);
        return ResultBody.ok("删除成功");
    }

    /**
     * 改
     **/
    @PostMapping("/updateById")
    public ResultBody<E> updateById(@RequestBody E entity) {
        baseService.updateById(entity);
        return ResultBody.ok(entity);
    }

    /**
     * 查
     **/
    @GetMapping("/getById")
    public ResultBody<?> getById(@RequestParam Long id) {
        return ResultBody.ok(baseService.getById(id));
    }

    /**
     * 存
     **/
    @PostMapping("/save")
    public ResultBody<E> save(@RequestBody E entity) {
        baseService.saveOrUpdate(entity);
        return ResultBody.ok(entity);
    }

    /**
     * 保存或更新
     **/
    @PostMapping("/saveOrUpdate")
    public ResultBody<E> saveOrUpdate(@RequestBody E entity) {
        baseService.saveOrUpdate(entity);
        return ResultBody.ok(entity);
    }

    /**
     * list查
     **/
    @PostMapping("/list")
    public ResultBody<List<E>> list(@RequestBody E entity) {
        QueryWrapper<E> queryWrapper = ApprenticeUtil.getQueryWrapper(entity);
        List<E> list = baseService.list(queryWrapper);
        return ResultBody.ok(list);
    }

    /**
     * page查
     **/
    @PostMapping("/page")
    public ResultBody<PageParams<E>> page(@RequestBody PageParams<E> pageParamDto) {
        if (pageParamDto == null) {
            return ResultBody.error("请求参数不能为空");
        }
        if (pageParamDto.getPage() < PAGE) {
            pageParamDto.setPage(PAGE);
        }
        if (pageParamDto.getSize() > SIZE) {
            pageParamDto.setSize(SIZE);
        }
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        LinkedList<OrderItem> orders = pageParamDto.getOrders();
        if (ObjectUtil.isNotEmpty(orders)) {
            orders.forEach(orderItem -> {
                if (BooleanUtil.isTrue(orderItem.isAsc())) {
                    queryWrapper.orderByAsc(orderItem.getColumn());
                }
                if (BooleanUtil.isFalse(orderItem.isAsc())) {
                    queryWrapper.orderByDesc(orderItem.getColumn());
                }
            });
        }
        PageParams<E> ePage = baseService.page(pageParamDto, queryWrapper);
        return ResultBody.ok(ePage);
    }

    /**
     * 获取数量
     **/
    @PostMapping("/count")
    public ResultBody<Long> count(@RequestBody E entity) {
        QueryWrapper<E> queryWrapper = ApprenticeUtil.getQueryWrapper(entity);
        long count = baseService.count(queryWrapper);
        return ResultBody.ok(count);
    }


}
